#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
为编程题添加测试用例
"""

from app import create_app
from models import db, Problem
import json

def add_test_cases():
    """为编程题添加测试用例"""
    app = create_app()
    
    with app.app_context():
        # 获取所有编程题
        code_problems = Problem.query.filter(Problem.type.in_(['code', 'code_completion'])).all()
        
        if not code_problems:
            print("没有找到编程题")
            return
        
        # 测试用例数据
        test_cases_data = {
            'Python基础语法': [
                {
                    'input': '5\n3',
                    'output': '8'
                },
                {
                    'input': '10\n20',
                    'output': '30'
                }
            ],
            'Python循环': [
                {
                    'input': '5',
                    'output': '1\n2\n3\n4\n5'
                },
                {
                    'input': '3',
                    'output': '1\n2\n3'
                }
            ],
            'Python函数': [
                {
                    'input': '5',
                    'output': '120'
                },
                {
                    'input': '0',
                    'output': '1'
                }
            ],
            'Python列表操作': [
                {
                    'input': '1 2 3 4 5',
                    'output': '15'
                },
                {
                    'input': '10 20 30',
                    'output': '60'
                }
            ],
            'Python字符串处理': [
                {
                    'input': 'hello world',
                    'output': 'HELLO WORLD'
                },
                {
                    'input': 'python',
                    'output': 'PYTHON'
                }
            ]
        }
        
        updated_count = 0
        for problem in code_problems:
            if problem.title in test_cases_data:
                problem.test_cases = json.dumps(test_cases_data[problem.title])
                updated_count += 1
                print(f"更新题目: {problem.title}")
            else:
                # 为没有预设测试用例的题目添加默认测试用例
                default_test_cases = [
                    {
                        'input': '1\n2',
                        'output': '3'
                    },
                    {
                        'input': '10\n20',
                        'output': '30'
                    }
                ]
                problem.test_cases = json.dumps(default_test_cases)
                updated_count += 1
                print(f"为题目添加默认测试用例: {problem.title}")
        
        try:
            db.session.commit()
            print(f"成功更新 {updated_count} 个编程题的测试用例")
        except Exception as e:
            db.session.rollback()
            print(f"更新测试用例失败: {e}")

if __name__ == '__main__':
    add_test_cases()
